package com.alibaba.algo.domain.user;

import com.alibaba.algo.cooperation.feign.vo.req.CheckUserPermissionsReqVO;
import com.alibaba.algo.cooperation.feign.vo.res.ResourceResVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.SelectResourceListByRoleReqVO;
import com.alibaba.algo.cooperation.usercomponent.vo.req.user.UpdateRoleResourceVO;
import com.alibaba.algo.core.base.BaseDomain;
import com.alibaba.algo.core.base.YesOrNo;
import com.alibaba.algo.core.exception.BaseException;
import com.alibaba.algo.dao.user.SysRoleResourceMapper;
import com.alibaba.algo.dtos.user.SysResourceTreeNodeDTO;
import com.alibaba.algo.entity.user.SysResource;
import com.alibaba.algo.entity.user.SysRoleResource;
import com.alibaba.algo.enums.SysResourceTypeEnum;
import com.alibaba.algo.exception.BaseErrorCode;
import com.alibaba.algo.util.DateConverterUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;

/**
 * 类SysRoleResource.java的实现描述：角色资源关联Domain
 *
 * @author nieganggang 2021-03-12 16:08:17
 */
@Component
public class SysRoleResourceDomain extends BaseDomain<SysRoleResource> {

    @Resource
    private SysRoleResourceMapper sysRoleResourceMapper;

    /**
     * 查询当前角色关联的菜单角色列表
     *
     * @param roleId 角色标识
     * @return 菜单列表
     */
    public List<SysRoleResource> selectMenuResourceListByRoleId(Long roleId) {
        if (ObjectUtils.isEmpty(roleId)) {
            throw new BaseException(BaseErrorCode.ROLE_ID_IS_NULL.getCode(), BaseErrorCode.ROLE_ID_IS_NULL.getMessage());
        }
        List<Integer> resourceTypeList = Arrays.asList(SysResourceTypeEnum.MENU.getValue());
        return sysRoleResourceMapper.selectMenuResourceListByRoleId(roleId, resourceTypeList);
    }

    public List<SysResource> selectResourceListByRoleId(Long roleId) {
        if (ObjectUtils.isEmpty(roleId)) {
            throw new BaseException(BaseErrorCode.ROLE_ID_IS_NULL.getCode(), BaseErrorCode.ROLE_ID_IS_NULL.getMessage());
        }
        return sysRoleResourceMapper.selectResourceListByRoleId(roleId);
    }

    /**
     * 更新关联关系，策略是找出需要删除的关系进行删除，找出需要新增的关系进行新增
     */
    public void updateRelatedByRoleId(Long roleId, List<Long> resourceIdList) {
        //现有的关系
        SysRoleResource param = new SysRoleResource();
        param.setRoleId(roleId);
        List<SysRoleResource> currentList = this.listByPara(param, null);

        if (resourceIdList != null) {
            out:
            for (Long resourceId : resourceIdList) {
                if (currentList != null) {
                    Iterator<SysRoleResource> iterator = currentList.iterator();
                    while (iterator.hasNext()) {
                        SysRoleResource temp = iterator.next();
                        if (resourceId.equals(temp.getResourceId())) {
                            iterator.remove();
                            continue out;
                        }
                    }
                }
                SysRoleResource roleResource = new SysRoleResource();
                roleResource.setRoleId(roleId);
                roleResource.setResourceId(resourceId);
                this.insert(roleResource);
            }
        }
        if (currentList != null) {
            for (SysRoleResource temp : currentList) {
                temp.setIsDeleted(YesOrNo.Y.name());
                this.updateById(temp);
            }
        }
    }

    public int deleteListByRole(UpdateRoleResourceVO vo){
        int result = sysRoleResourceMapper.deleteListByRole(vo);
        return result;
    }

    public void batchInsert(Long roleId, List<Long> resourceIdList) {
        LocalDateTime cutDateTime = DateConverterUtils.asLocalDateTime(new Date());
        List<SysRoleResource> sysRoleResourceList = new ArrayList<>();
        for (Long resourceId : resourceIdList) {
            SysRoleResource sysRoleResource = new SysRoleResource();
            sysRoleResource.setResourceId(resourceId);
            sysRoleResource.setRoleId(roleId);
            sysRoleResource.setIsDeleted(YesOrNo.N.name());
            sysRoleResource.setGmtCreated(cutDateTime);
            sysRoleResource.setGmtModified(cutDateTime);
            sysRoleResourceList.add(sysRoleResource);
        }
        sysRoleResourceMapper.batchInsert(sysRoleResourceList);
    }

    public List<SysResourceTreeNodeDTO> selectResourceListByRoleIdAndAppCode(Long roleId){
        return sysRoleResourceMapper.selectResourceListByRoleIdAndAppCode(roleId);
    }

    public List<SysResourceTreeNodeDTO> selectResourceListByRoleIdListAndAppCode(SelectResourceListByRoleReqVO vo){
        return sysRoleResourceMapper.selectResourceListByRoleIdListAndAppCode(vo);
    }

    public List<ResourceResVO> selectResourceListByRoleAppPath(CheckUserPermissionsReqVO vo){
        return sysRoleResourceMapper.selectResourceListByRoleAppPath(vo);
    }
}
